package com.haoqizhe.kernel.activemq.connection;

import com.haoqizhe.kernel.activemq.properties.ActivemqProperties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jms.activemq.ActiveMQProperties;
import org.springframework.util.StringUtils;

import java.lang.reflect.InvocationTargetException;

/**
 *
 *  创建ActiveMQConnectionFactory的工厂类
 *
 * @author  haoqizhe.li on 2017/6/12.
 */
public class ActiveMQConnectionFactoryFactory {

    @Value("${spring.activemq.broker-url}")
    private String activemqUrl;

    private final ActivemqProperties properties1;

    private final ActiveMQProperties properties;

    public ActiveMQConnectionFactoryFactory(ActiveMQProperties properties,ActivemqProperties properties1){

        this.properties = properties;
        this.properties1 = properties1;
    }

    public <T extends org.apache.activemq.ActiveMQConnectionFactory> T createConnectionFactory(Class<T> factoryClass) {
        try {
            return doCreateConnectionFactory(factoryClass);
        } catch (Exception ex) {
            throw new IllegalStateException("Unable to create ActiveMQConnectionFactory", ex);
        }
    }

    public <T extends org.apache.activemq.ActiveMQConnectionFactory> T doCreateConnectionFactory(Class<T> factoryClass)throws
            InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException{
        org.apache.activemq.ActiveMQConnectionFactory factory = createConnectionFactoryInstance(factoryClass);
        ActiveMQProperties.Packages packages = this.properties.getPackages();
        if (packages.getTrustAll() != null) {
            factory.setTrustAllPackages(packages.getTrustAll().booleanValue());
        }
        if (!(packages.getTrusted().isEmpty())) {
            factory.setTrustedPackages(packages.getTrusted());
        }
        return (T) factory;

    }

    private <T extends org.apache.activemq.ActiveMQConnectionFactory> T createConnectionFactoryInstance(Class<T> factoryClass)
            throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        String brokerUrl = determineBrokerUrl();
        String user = this.properties.getUser();
        String password = this.properties.getPassword();
        if ((StringUtils.hasLength(user)) && (StringUtils.hasLength(password))) {
            return (T) ((org.apache.activemq.ActiveMQConnectionFactory) factoryClass
                    .getConstructor(new Class[] { String.class, String.class, String.class })
                    .newInstance(new Object[] { user, password, brokerUrl }));
        }

        return (T) ((org.apache.activemq.ActiveMQConnectionFactory) factoryClass.getConstructor(new Class[] { String.class })
                .newInstance(new Object[] { brokerUrl }));
    }

    String determineBrokerUrl() {
        if (this.properties.getBrokerUrl() != null) {
            return this.properties.getBrokerUrl();
        }
        if (this.properties.isInMemory()) {
            return properties1.getBrokerUrl();
        }
        return this.activemqUrl;
    }

}
